package com.amap.builder;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.amap.config.AmapMerchantConfig;
import com.amap.constants.Constants;
import com.amap.utils.BeanUtils;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author liuyi
 * @since 2025/10/31 9:10
 */
public class AmapParamBuilder {
    private final Map<String, String> params = new HashMap();
    private final JSONObject bizContent = new JSONObject();

    public AmapParamBuilder() {
        this.params.put("version", "1.0");
        this.params.put("charset", "UTF-8");
        this.params.put("utc_timestamp", String.valueOf(System.currentTimeMillis()));
    }

    public AmapParamBuilder method(String method) {
        this.params.put("method", method);
        return this;
    }

    public AmapParamBuilder putBeanToBizParam(Object bean) {
        this.bizContent.putAll(BeanUtils.beanToMap(bean));
        return this;
    }

    public AmapParamBuilder putMapToBizParam(Map<String, Object> map) {
        this.bizContent.putAll(map);
        return this;
    }

    public Map<String, String> build(AmapMerchantConfig config) {
        this.params.put(Constants.PARAM_APP_ID, config.getAppId());
        this.params.put(Constants.PARAM_SIGN_TYPE, config.getSignType());
        this.params.put(Constants.PARAM_BIZ_CONTENT, this.bizContent.toJSONString(new JSONWriter.Feature[0]));
        return this.params;
    }
}
